﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace LanDBom.Models
{
    public class find_appliction
    {
        LDbaseEntities LD = new LDbaseEntities();
        public List<Applications> find_Contracts(string BB_SQ_xmmc, string BB_SQ_khmc, string BB_SQ_gjz,string jssj,string qssj , string danhao,string xmlx,string jzrq)
        {
            List<Applications> index = LD.Applications.Where(s=>s.AP_bian.Contains("SQ")).OrderByDescending(s=>s.App_qishi).ToList();
            foreach (var i in index)
            {
                if (i.App_JTime == null)
                {
                    i.App_JTime = "1/11/3333 12:00:00 AM"; ;
                }
                if (i.App_qishi == null)
                {
                    i.App_qishi = "1/11/1111 12:00:00 AM";
                }
                if (i.APP_name == null)
                {
                    i.APP_name = "";
                }
                if (i.App_Kehu == null)
                {
                    i.App_Kehu = "";
                }
                if (i.APP_Zhong == null)
                {
                    i.APP_Zhong = "";
                }
                if (i.AP_bian == null)
                {
                    i.AP_bian = "";
                }
            }
            if (danhao != null)
            {
                index = index.Where(x => x.AP_bian.Contains(danhao)).ToList();
            }
            if (BB_SQ_xmmc != "")
            {
                index = index.Where(x => x.APP_name.Contains(BB_SQ_xmmc)).ToList();
            }
            if (BB_SQ_khmc != "")
            {
                index = index.Where(x => x.App_Kehu.Contains(BB_SQ_khmc)).ToList();
            }
            if (BB_SQ_gjz != null&& BB_SQ_gjz!="")
            {
                index = index.Where(x => x.AP_bian.Contains(BB_SQ_gjz)|| x.APP_name.Contains(BB_SQ_gjz) || x.App_Kehu.Contains(BB_SQ_gjz) || x.APP_Zhong.Contains(BB_SQ_gjz) || x.App_qishi.Contains(BB_SQ_gjz) || x.App_JTime.Contains(BB_SQ_gjz)).ToList();
            }
            if (jssj != "")
            {
                DateTime result = DateTime.Parse(jssj);
                index = index.Where(x => DateTime.Parse(x.App_qishi) <= result).ToList();
            }
            if (qssj != ""&& qssj!=null)
            {
                DateTime result = DateTime.Parse(qssj);
                index = index.Where(x => DateTime.Parse(x.App_qishi) >= result).ToList();
            }
            if (jzrq != null && jzrq != "")
            {
                DateTime result = DateTime.Parse(jzrq);
                index = index.Where(x => DateTime.Parse(x.App_JTime) <= result).ToList();
            }
            if (xmlx != "0")
            {
                if (xmlx == "WON Opportunities")
                {
                    index = index.Where(x => x.App_xmzt == "WON Opportunities").ToList();
                }
                else if(xmlx== "Open Opportunities")
                {
                    index = index.Where(x => x.App_xmzt == "OPEN Opportunities").ToList();
                }
                else if (xmlx == "LOST Opportunities")
                {
                    index = index.Where(x => x.App_xmzt == "LOST Opportunities").ToList();
                }
            }
            index = index.OrderByDescending(s => DateTime.Parse(s.App_qishi)).ToList();
            return index;
        }

        public List<Applications> find_Contracts2(string BB_SQ_xmmc, string BB_SQ_khmc, string BB_SQ_sqrq, string BB_SQ_gjz)
        {
            List<Applications> index = LD.Applications.Where(s=>s.App_bei=="保存"||s.App_zhuang=="驳回").ToList();
            index = index.Where(s => s.App_bu == "1"&&s.AP_bian.Contains("SQ")&&(s.AP_QTJY==null||s.AP_QTJY=="")).ToList();
            foreach (var i in index)
            {
                if (i.App_JTime == null)
                {
                    i.App_JTime = "";
                }
                if (i.App_qishi == null)
                {
                    i.App_qishi = "";
                }
                if (i.APP_name == null)
                {
                    i.APP_name = "";
                }
                if (i.App_Kehu == null)
                {
                    i.App_Kehu = "";
                }
                if (i.APP_name == null)
                {
                    i.APP_name = "";
                }
                if (i.APP_Zhong == null)
                {
                    i.APP_Zhong = "";
                }
            }
            if (BB_SQ_xmmc != "")
            {
                index = index.Where(x => x.APP_name.Contains(BB_SQ_xmmc)).ToList();
            }
            if (BB_SQ_khmc != "")
            {
                index = index.Where(x => x.App_Kehu.Contains(BB_SQ_khmc)).ToList();
            }
            if (BB_SQ_sqrq != "")
            {
                index = index.Where(x => x.App_qishi.Contains(BB_SQ_sqrq)).ToList();
            }
            if (BB_SQ_gjz != null)
            {
                index = index.Where(x => x.AP_bian.Contains(BB_SQ_gjz) || x.APP_name.Contains(BB_SQ_gjz) || x.App_Kehu.Contains(BB_SQ_gjz) || x.APP_Zhong.Contains(BB_SQ_gjz) || x.App_qishi.Contains(BB_SQ_gjz) || x.App_JTime.Contains(BB_SQ_gjz)).ToList();
            }
            return index;
        }

        public List<Applications> find_DHLY(string bian)
        {
            List<Applications> index = LD.Applications.Where(x=>x.AP_bian==bian).ToList();
            return index;
        }
    }
}